package com.example.fitnessservice.api.web;

import com.example.fitnessservice.module.pojo.Result;
import com.example.fitnessservice.module.pojo.UserInfo;
import com.example.fitnessservice.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/our")
//个人中心里面的个人信息
public class MyController {
    @Autowired
    private MyService myService;

    /**
     * 查询用户的个人信息
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    public Result findMyInfo(@PathVariable Integer userId){
        UserInfo userInfo = myService.findMyInfo(userId);
        System.out.println(userInfo);

        Result result = new Result();
        if(userInfo != null){
            result.setCode(200);
            result.setMsg("查询用户信息成功");
            result.setData(userInfo);
        }else {
            result.setCode(500);
            result.setMsg("您还未填写您的个人信息，请去填写吧");
        }
        return result;
    }

    /**
     * 添加用户个人信息
     * @param userInfo
     * @return
     */
    @PostMapping("/{useId}")
    public Result addMyInfo(@RequestBody UserInfo userInfo, @PathVariable String useId){
        userInfo.setUserId(Integer.parseInt(useId));
        boolean flag = myService.addMyInfo(userInfo);

        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("添加用户信息成功");

        }else {
            result.setCode(500);
            result.setMsg("添加用户信息失败");
        }
        return result;
    }

    /**
     * 修改用户的个人信息
     * @param userInfo
     * @return
     */
    @PutMapping
    public Result updateMyInfo(@RequestBody UserInfo userInfo){
        System.out.println(userInfo);
        boolean flag = myService.updateMyInfo(userInfo);

        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("修改用户信息成功");

        }else {
            result.setCode(500);
            result.setMsg("修改用户信息失败");
        }
        return result;
    }
}
